package com.lxs;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CommandLineNetworkTools {
    public static void main(String[] args) {
        runNetworkCommands();
    }

    public static void runNetworkCommands() {
        try {
            // 执行 arp 命令查看ARP表
            System.out.println("=== ARP表（已知设备）===");
            Process process;
            if (System.getProperty("os.name").toLowerCase().contains("windows")) {
                process = Runtime.getRuntime().exec("arp -a");
            } else {
                process = Runtime.getRuntime().exec("arp -a");
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 执行 netstat 查看网络连接
            System.out.println("\n=== 网络连接 ===");
            if (System.getProperty("os.name").toLowerCase().contains("windows")) {
                process = Runtime.getRuntime().exec("netstat -n");
            } else {
                process = Runtime.getRuntime().exec("netstat -tuln");
            }

            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
